﻿using System;
using System.IO;

namespace MEF_Test.Bricks.Subtitles
{
    using System.Collections.Generic;
    using System.ComponentModel;

    using MEF_Test.Contracts;

    public abstract class SubtitlesProviderBase : ISubtitlesProvider
    {
        protected ISubtitleFormatHandlersProvider SubtitleFormatHandlersProvider { get; set; }

        protected SubtitlesProviderBase(ISubtitleFormatHandlersProvider subtitleFormatHandlersProvider)
        {
            SubtitleFormatHandlersProvider = subtitleFormatHandlersProvider;
        }

        /// <summary>
        /// Gets the subtitles async.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <returns></returns>
        public SubtitlesProviderResults GetSubtitlesAsync(IBrowserNodeViewModel node)
        {
            var results = new SubtitlesProviderResults();       
            var backgroundWorker = new BackgroundWorker();

            // Only the latest update received will be considered.
            backgroundWorker.DoWork += (o, e) =>
                {
                    results.IsBusy = true;
                    results.Subtitles = this.GetSubtitles((IBrowserNodeViewModel)e.Argument);
                    e.Result = results;
                };

            backgroundWorker.RunWorkerCompleted += (o, args) =>
                {
                    results.IsBusy = false;
                    if (args.Error != null)
                    {
                        results.Subtitles = new List<ISubtitleViewModel>();
                        results.IsSuccessful = false;
                    }
                    else
                    {
                        results.Subtitles = ((SubtitlesProviderResults)args.Result).Subtitles;
                    }
                };

            backgroundWorker.RunWorkerAsync(node);

            return results;
        }

        ///// <summary>
        ///// Gets the subtitles.
        ///// </summary>
        ///// <param name="filename">The filename.</param>
        ///// <returns>The subtitles description</returns>
        //public abstract IEnumerable<ISubtitleViewModel> GetSubtitlesList(string filename);

        /// <summary>
        /// Gets the name of the provider.
        /// </summary>
        /// <returns>The name of the subtitles provider.</returns>
        public abstract string GetProviderName();

        /// <summary>
        /// Gets the subtitles.
        /// </summary>
        /// <param name="node">The media node.</param>
        /// <returns>
        /// The subtitles descriptions grouped by provider
        /// </returns>
        public abstract IEnumerable<ISubtitleViewModel> GetSubtitles(IBrowserNodeViewModel node);

        /// <summary>
        /// Downloads the subtitle file.
        /// </summary>
        /// <param name="subtitleViewModel">The subtitle view model.</param>
        /// <returns>
        /// A stream to the content of the subtitle file.
        /// </returns>
        public abstract Stream OpenSubtitleFile(int subtitleViewModel);
    }
}